$(function () {
    fy(1, 3);
    xia();
})

function xia() {
    $.post("/beyond/selectGrade", function (data) {
        $("#cp").empty();
        $("#cp").append('<option value="0" selected="selected">选择年级</option>');
        $.each(data, function (index, xia) {
            $("#cp").append('<option value="' + xia.id + '">' + xia.grade + '</option>')
        });
        /*   $.each(data.list, function (xia) {
               console.log(xia)
               $(".input").append(" <option value=\"\">" + xia.gradeName + "</option>")
           })*/
    })
    $.post("/beyond/selectCourse", function (data) {
        $("#cp1").empty();
        $("#cp1").append('<option value="0" selected="selected">选择课程</option>');
        $.each(data, function (index, xia) {
            $("#cp1").append('<option value="' + xia.id + '">' + xia.coursename + '</option>')
        });
    })
}


function tb() {
    $(".search").append("  <li>\n" +
        "                <a class=\"button border-yellow\" href=\"video.html\"><span class=\"icon-plus-square-o\"></span> 添加视屏</a></li>\n" +
        "            <li>\n" +
        "                <select name=\"cid\" class=\"input\" id='cp' style=\"width:200px; line-height:17px;\" onchange=\"changesearch()\">\n" +
        "                    <option value=\"\">选择年级</option>\n" +
        "                    <option value=\"\">产品分类</option>\n" +
        "                    <option value=\"\">产品分类</option>\n" +
        "                    <option value=\"\">产品分类</option>\n" +
        "                    <option value=\"\">产品分类</option>\n" +
        "                </select>\n" +
        "            </li>\n" +
        "            <li>\n" +
        "                <select name=\"cid\" class=\"input\" id='cp1' style=\"width:200px; line-height:17px;\" onchange=\"changesearch()\">\n" +
        "                    <option value=\"\">选择课程</option>\n" +
        "                    <option value=\"\">产品分类</option>\n" +
        "                    <option value=\"\">产品分类</option>\n" +
        "                    <option value=\"\">产品分类</option>\n" +
        "                    <option value=\"\">产品分类</option>\n" +
        "                </select>\n" +
        "            </li>\n" +
        "            <li>\n" +
        "                <select name=\"cid\" class=\"input\" style=\"width:200px; line-height:17px;\" onchange=\"changesearch()\">\n" +
        "                    <option value=\"\">按销量排序</option>\n" +
        "                    <option value=\"\">由低到高</option>\n" +
        "                    <option value=\"\">由高到低</option>\n" +
        "                </select>\n" +
        "            </li>\n" +
        "            <li>\n" +
        "                <input type=\"text\" placeholder=\"请输入搜索关键字\" name=\"keywords\" class=\"input\"\n" +
        "                       style=\"width:250px; line-height:17px;display:inline-block\"/>\n" +
        "                <a href=\"javascript:void(0)\" class=\"button border-main icon-search\" onclick=\"changesearch()\"> 搜索</a>\n" +
        "            </li>")
    $(".table").append(" <tr>\n" +
        "            <th width=\"5%\">编号</th>\n" +
        "            <th>题目名称</th>\n" +
        "            <th>所属课程</th>\n" +
        "            <th>所属年级</th>\n" +
        "            <th width=\"250\">操作</th>\n" +
        "        </tr>")

}


function fy(pageNum, pageSize) {
    tb();
    $.post("/beyond/selectQue", {
        "pageNum": pageNum,
        "pageSize": pageSize
    }, function (data) {
        console.log(data)
        $.each(data.list, function (i, cha) {
            $(".table").append("  <tr>\n" +
                "            <td>" + (i + 1) + "</td>\n" +
                "            <td>" + cha.content + "</td>\n" +
                "            <td>" + cha.coursename + "</td>\n" +
                "            <td>" + cha.gradename + "</td>\n" +
                "            <td>\n" +
                "                <div class=\"button-group\">\n" +
                "                    <a type=\"button\" class=\"button border-main ty\" href=\"#\"  data-id='" + cha.id + "'><span class=\"icon-edit\"></span>上架</a>\n" +
                "                    <a class=\"button border-red bty\" href=\"javascript:void(0)\" data-id='" + cha.id + "'><span\n" +
                "                            class=\"icon-trash-o\"></span>下架</a>\n" +
                "                </div>\n" +
                "            </td>\n" +
                "        </tr>")
        })
        $(".table").append(" <tr>\n" +
            "                <td colspan=\"10\"><div class=\"pagelist\"></div></td>\n" +
            "            </tr>")

        $(".pagelist").empty();
        if (data.pageNum > 1) {
            $(".pagelist").append('<a href="javascript:void(0)" class="curs"  id="1" >首页</a>')
        }
        if (data.pageNum - 1 >= 1) {
            $(".pagelist").append('<a href="javascript:void(0)" class="curs"  id="' + (data.pageNum - 1) + '" >上一页</a>')
        }
        $.each(data.navigatepageNums, function (index, fy) {
            if (data.pageNum == fy) {
                $(".pagelist").append(
                    "<a href=\"javascript:void(0)\" style='background-color: skyblue' class=\"curs\" id='" + fy + "'>" + fy + "</a>"
                )
            } else {
                $(".pagelist").append(
                    "<a href=\"javascript:void(0)\" class=\"curs\" id='" + fy + "'>" + fy + "</a>"
                )

            }

        })
        if (data.pageNum < data.pages) {
            $(".pagelist").append('<a href="javascript:void(0)" class="curs"  id="' + (data.pageNum + 1) + '" >下一页</a>')
        }
        if (data.pageNum < data.pages) {
            $(".pagelist").append('<a href="javascript:void(0)" class="curs"  id="' + data.pages + '" >末页</a>')
        }
        $(".curs").click(function () {
            $(".search").empty();
            $(".table").empty();
            /*  tb();*/
            xia();
            fy($(this).attr("id"), 3)
        })
        $(".ty").click(function () {
            var id = $(this).data("id");
            $.post("/beyond/updateQueTy", {"id": id}, function (data) {
                $(".search").empty();
                fy(1, 3);
                $(".table").empty();

            })
        })
        $(".bty").click(function () {
            var id = $(this).data("id");
            $.post("/beyond/updateQueBty", {"id": id}, function (data) {
                $(".search").empty();
                fy(1, 3);
                /*       $(".search").empty();*/
                $(".table").empty();
            })
        })

        var dd = data.list == 0;
        if (dd) {
            $(".table").empty();
            $(".table").append("<tr>\n" +
                "            <td style='font-size: 16px;color: red'><span style='text-align: center'>暂无数据</span></td>\n" +
                "        </tr>")
        }
    })

}
